package common;

import org.apache.commons.lang.builder.ToStringStyle;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @ClassName ToStringStyle
 * @Description TODO
 * @Author fengk
 * @Date 2018/8/27 00:00
 */

public class MyToStringStyle extends org.apache.commons.lang.builder.ToStringStyle {
    private static final String DEFAULT_TIME="yyyy-MM-dd HH:mm:ss";
    private static final String DEFAULT_DAY="yyyy-MM-dd";
    public static final ToStringStyle TIME_STYLE=new DateStyle(MyToStringStyle.DEFAULT_TIME);
    public static final ToStringStyle DAY_STYLE=new DateStyle(MyToStringStyle.DEFAULT_DAY);
    public static final ToStringStyle DEFAULT_STYLE=MyToStringStyle.TIME_STYLE;
    private static class DateStyle extends ToStringStyle{
        private String pattern;
        public DateStyle(String pattern){
            super();
            this.setUseShortClassName(true);
            this.setUseIdentityHashCode(false);
            this.pattern=pattern;
        }
        protected void appendDetail(StringBuffer buffer,String fieldName,Object value){
            if(value instanceof Date){
                value=new SimpleDateFormat(pattern).format(value);
            }
            buffer.append(value);
        }
    }
}
